/**
 * Copyright (c) 2006 RiseSoft Co.,Ltd  
 * $Header$
 */
package cn.newtest.bi.collection.model;

import java.io.Serializable;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.GenericGenerator;

/**
 * 分类，类似于以前的字典
 *
 * @author <a href="mailto: chenjian@risesoft.net">陈健</a>
 * @version $Revision$
 * 
 */
@Entity
@org.hibernate.annotations.Entity(dynamicInsert=true,dynamicUpdate=true)
@Table(name="Rpt_Catalog")
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class Catalog implements Comparable<Object>, Serializable {
	
	private static final long serialVersionUID = -5134964607179646486L;
	
	@Id
	@Column(name="CataLog_GUID",length=38)
	@GenericGenerator(name="Catalog-assigned",strategy="assigned")
	private String guid;

	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="App_GUID")
	private Application application; 
	
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="service_GUID")
	private Service service;
	
	/**
	 * 该catalog是否是so，是so则有基本情况样式
	 */
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="template_GUID",nullable=true)
	private Template template;
	
	@Lob
	private String hbm;
	
	/**
	 * 分类的代码。只能是字母和数字，系统会据此生成数据库表，
	 *	形式为：Rpt_Catalog_XXXX，该表中包含几个固定字段：
	 *	Item_Code,Item_Name，Item_index,。。。
	 *	用户可以任意添加需要的字段。
	 */
	@Column(name="CataLog_Code",length=20)
	private String code;
	
	@Column(length=30)
	private String tableName;
	
	@Column(name="CataLog_Name",length=50)
	private String name;
	
	@Basic
	private Double tabIndex;
	
	/**
	 * 1=表示统计调查对象类型
	 * 2=指标自由项对应的主表
	 * 3=指标区域对应的细表
	 * 4=指标目录与分类
	 * 5=数据字典
	 */
	@Column(name="CataLog_Type")
	private Integer type;
	
	@Column(name="isEnabled")
	private Boolean enabled;
	
	@Column(name="isVirtual")
	private Boolean virtual;
	
	/**
	 * 0:无父子关系
	 * 1:邻接模型，父子ID的邻接模型：用id和parentid构成父子关系,
	 *              id字段为：item_guid
	 *              parentid字段为：item_parentguid
	 * 2:分层编码模型，一般由代码字段(item_code)分段编码，各段的长度可以不同，构成树状结构，
	 *              相同层的编码长度相同，  不同层的编码长度可以不同，
	 *              但下一层的编码包含上一层的编码，
	 *              具体的字段由hierarchical_field_name指定。
	*/
	@Column(name="Hierarchical_Type")
	private Integer hierarchicalType;  
	
	//hierarchicalType=2时设置。
	@Column(name="hierarchical_field_name")
	private  String hierarchical_field_name; 
	
	//hierarchicalType=2时设置。
	@Column(name="segmentslength")
	private  String segmentslength;
	/**
	 * 各段的长度之间用逗号分隔，如“2，3，2，4”，
	 * 表示第一层2位编码，第二层3位编码，第三层2位编码，第四层4位编码，
	 * 这样第四层编码字段的实际形式为：AABBBCCDDDD 
	 */	

	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="so_type",nullable=true)
	private SoType soType;
	
	@Transient
	public String getSoTypeGuid() {
		if (soType != null) {
			return soType.getGuid();
		}
		return "";
	}
	
	public void setSoTypeGuid(String soTypeGuid) {
		if (soTypeGuid != null && soTypeGuid.trim().length() > 0) {
			if (soType == null) {
				soType = new SoType();
				soType.setGuid(soTypeGuid);
			}else{
				soType.setGuid(soTypeGuid);
			}
		}
	}
	
	@Transient
	public String getAppGuid() {
		if (application != null) {
			return application.getGuid();
		}
		return "";
	}
	
	public void setAppGuid(String appGuid) {
		if (appGuid != null && appGuid.trim().length() > 0) {
			if (application == null) {
				application = new Application();
			}
			application.setGuid(appGuid);			
		}
	}
	
	@Transient
	public String getServiceGuid() {
		if (service != null) {
			return service.getGuid();
		}
		return "";
	}
	
	public void setServiceGuid(String serviceGuid) {
		if (serviceGuid != null && serviceGuid.trim().length() > 0) {
			if (service == null) {
				service = new Service();
			}
			service.setGuid(serviceGuid);			
		}
	}
	
	@Transient
	public String getTemplateName() {
		if (template != null) {
			return template.getName();
		}
		return "";
	}
	
	@Transient
	public String getTemplateCode() {
		if (template != null) {
			return template.getCode();
		}
		return "";
	}
	
	@Transient
	public String getTemplateGuid() {
		if (template != null) {
			return template.getGuid();
		}
		return "";
	}
	
	public void setTemplateGuid(String templateGuid) {
		if (templateGuid != null && templateGuid.trim().length() > 0) {
			if (template == null) {
				template = new Template();
			}
			template.setGuid(templateGuid);			
		}
	}
	
	@Transient
	public Serializable getId() {
		return getGuid();
	}
	
	public void setId(Serializable id) {
		setGuid((String)id);
	}
	
	public String getHbm() {
		return hbm;
	}
	
	public void setHbm(String hbm) {
		this.hbm = hbm;
	}
	
	public Template getTemplate() {
		return template;
	}
	
	public void setTemplate(Template template) {
		this.template = template;
	}
	
	public Application getApplication() {
		return application;
	}

	public void setApplication(Application application) {
		this.application = application;
	}
	
	public SoType getSoType() {
		return soType;
	}

	public void setSoType(SoType soType) {
		this.soType = soType;
	}

	public Boolean getEnabled() {
		return enabled;
	}

	public void setEnabled(Boolean enabled) {
		this.enabled = enabled;
	}

	public Integer getHierarchicalType() {
		return hierarchicalType;
	}

	public void setHierarchicalType(Integer hierarchicalType) {
		this.hierarchicalType = hierarchicalType;
	}

	public Integer getType() {
		return type;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public String getGuid() {
		return guid;
	}

	public void setGuid(String guid) {
		this.guid = guid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Double getTabIndex() {
		return tabIndex;
	}

	public void setTabIndex(Double tabIndex) {
		this.tabIndex = tabIndex;
	}

	public Service getService() {
		return service;
	}
	
	public void setService(Service service) {
		this.service = service;
	}

	public Boolean getVirtual() {
		return virtual;
	}

	public void setVirtual(Boolean virtual) {
		this.virtual = virtual;
	}

	public String getHierarchical_field_name() {
		return hierarchical_field_name;
	}

	public void setHierarchical_field_name(String hierarchical_field_name) {
		this.hierarchical_field_name = hierarchical_field_name;
	}

	public String getSegmentslength() {
		return segmentslength;
	}

	public void setSegmentslength(String segmentslength) {
		this.segmentslength = segmentslength;
	}

	/**
	 * @see java.lang.Comparable#compareTo(Object)
	 */
	public int compareTo(Object object) {
		Catalog myClass = (Catalog) object;
		return new CompareToBuilder().append(this.code, myClass.code).append(this.type, myClass.type).append(this.enabled, myClass.enabled).append(this.guid, myClass.guid).append(this.tabIndex, myClass.tabIndex).append(this.name, myClass.name).append(this.hierarchicalType, myClass.hierarchicalType)
				.toComparison();
	}

	/**
	 * @see java.lang.Object#equals(Object)
	 */
	public boolean equals(Object object) {
		if (!(object instanceof Catalog)) {
			return false;
		}
		Catalog rhs = (Catalog) object;
		return new EqualsBuilder().appendSuper(super.equals(object)).append(this.code, rhs.code).append(this.type, rhs.type).append(this.enabled, rhs.enabled).append(this.guid, rhs.guid).append(this.tabIndex, rhs.tabIndex).append(this.name, rhs.name).append(this.hierarchicalType, rhs.hierarchicalType)
				.isEquals();
	}

	/**
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		return new HashCodeBuilder(268439365, 1596374195).appendSuper(super.hashCode()).append(this.code).append(this.type).append(this.enabled).append(this.guid).append(this.tabIndex).append(this.name).append(this.hierarchicalType).toHashCode();
	}

	/**
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return new ToStringBuilder(this).append("tabIndex", this.tabIndex).append("name", this.name).append("id", this.getId()).append("hierarchicalType", this.hierarchicalType).append("code", this.code).append("enabled", this.enabled).append("type", this.type).append("guid", this.guid).toString();
	}
}